package com.bluedon.ism.sensitive;

import java.util.ArrayList;
import java.util.List;

public class SensitiveBuilder {
	private static Node root;
	private List<? extends CharSequence> keywords=new ArrayList<CharSequence>();
	private Object lock=new Object();
	
	private void buildNodeTree() {
		root=new Node('R', 1);
		int index=0;
		int size=0;
		Node node=null,currentNode=null;
		for(CharSequence cs:keywords) {
			if(cs==null||cs.length()==0)
				continue;
			index=0;
			size=cs.length();
			node=root;
			while(index<size) {
				char c=cs.charAt(index);
				currentNode=Node.findNode(node,c);
				if(currentNode==null) {
					currentNode=new Node(c, index==size-1?1:0);
					node.children.add(currentNode);
					if(node.flag==1)
						node.flag=0;
				}
				node=currentNode;
				index++;
			}
		}
	}
	
	
	public Matcher getMatcher(String content) {
		synchronized (lock) {
			if(root==null)
				buildNodeTree();
		}
		Matcher matcher=new Matcher(root);
		matcher.process(content);
		return matcher;
	}

	public List<? extends CharSequence> getKeywords() {
		return keywords;
	}

	public void setKeywords(List<? extends CharSequence> keywords) {
		this.keywords = keywords;
	}
}
